package com.csun.cmny.provider.web.frontend;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OpcFlowMessage;
import com.csun.cmny.provider.model.domain.OpcFlowMessageWay;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.model.vo.message.MessageWayVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OpcFlowMessageService;
import com.csun.cmny.provider.service.OpcFlowMessageWayService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 10:02
 * @Description:
 */
@RestController
@RequestMapping(value = "/flowMessage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OpcFlowMessageController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OpcFlowMessageService opcFlowMessageService;
    @Resource
    private OpcFlowMessageWayService opcFlowMessageWayService;

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Wrapper add(@RequestBody OpcFlowMessage omcFlowMessage) {

        Integer result = opcFlowMessageService.add(omcFlowMessage, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 修改
     */
    @PostMapping(value = "/edit")
    public Wrapper edit(@RequestBody OpcFlowMessage omcFlowMessage) {

        Integer result = opcFlowMessageService.edit(omcFlowMessage, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = opcFlowMessageService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 查询 - 分页
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody FlowMessageQueryDto flowMessageQueryDto) {

        List<OpcFlowMessage> list = opcFlowMessageService.queryListWithPage(flowMessageQueryDto);

        //return WrapMapper.ok(new PageInfo<>(list));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), new PageInfo<>(list));
    }

    /**
     * 绑定推送方式
     */
    @PostMapping(value = "/bind")
    public Wrapper bind(@RequestBody OpcFlowMessageWay opcFlowMessageWay) {

        Integer result = opcFlowMessageWayService.bind(opcFlowMessageWay);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 解绑推送方式
     */
    @PostMapping(value = "/untied")
    public Wrapper untied(@RequestBody OpcFlowMessageWay opcFlowMessageWay) {

        Integer result = opcFlowMessageWayService.untied(opcFlowMessageWay);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 查询推送方式 -- 根据Id
     */
    @PostMapping(value = "/queryWayListById/{id}")
    public Wrapper queryWayListById(@PathVariable Long id) {

        List<MessageWayVo> list = opcFlowMessageWayService.queryWayListById(id);

        //return WrapMapper.ok(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), list);
    }


}
